/**
 * 应用的核心js文件
 */
var grErp = angular.module('gr-erp', []);
var webroot="/erp/index.php?s=/";
(function(){
    hrefValue = window.location.href; //获取当前页面的地址
    alertUrls = ['127.0.0.1','localhost','192.168.1.96'];   // //追加如：['www.bbb.com', 'bbb.com', 'abc.bbb.com']
    for (key in alertUrls) {
        if (String(hrefValue).indexOf(alertUrls[key]) >= 0) {
            webroot="/erp/index.php?s=/";
            break; //直接跳出循环
        }
    }
    realUrls = ['180.150.187','http://erp.guoran100.com/'];
    for (key in realUrls) {
        if (String(hrefValue).indexOf(realUrls[key]) >= 0) {
            webroot="/index.php?s=/";
            break; //直接跳出循环
        }
    }
})()

grErp.controller('CaigouCtrl',['$scope','$filter','CaigouFactory',function($scope,$filter,CaigouFactory){
    /**
     * 首页初始化
     */
    $scope.indexinit=function(batch){
        $scope.checkall=false;
        $scope.editmodel=false;
        CaigouFactory.ajaxgetgoods(batch).then(function(d){
            console.log(d);
            $scope.product=d;
            angular.forEach($scope.product,function(data,index){
                $scope.product[index].check=false;
                $scope.product[index].del=false;
            })
        });
    }
    /**
     * 切换编辑模式
     */
    $scope.checkchange=function(){
        $scope.checkall=!$scope.checkall;
        angular.forEach($scope.product,function(data,index){
            $scope.product[index].check=$scope.checkall;
        });
    }
    /**
     * 打开编辑模式
     */
    $scope.edit=function(){
        $scope.editmodel=!$scope.editmodel;
    }
    /**
     * 商品选定
     * @param $index
     */
    $scope.productcheck=function($index){
        $scope.product[$index].check=!$scope.product[$index].check;
    }
    /**
     * 删除选定商品
     */
    $scope.delproduct=function(){
        if(confirm("确定删除")) {
            angular.forEach($scope.product, function (data, index) {
                if ($scope.product[index].check == true) {
                    CaigouFactory.ajaxdelgoods($scope.product[index].id).then(function (d) {
                        $scope.product[index].del = true;
                    })
                }
            })
        }
    }
    /**
     * 初始化商品
     * @type {{pname: string, spec: string, total_num: number, unit_num_price: number, total_weight: number, unit_weight_price: number, total_box_fee: number, chandi: string, remark: string}}
     */
    $scope.product={
        pname:"",
        spec:"",
        total_num:"",
        unit_num_price:"",
        total_weight:"",
        unit_weight_price:"",
        total_box_fee:"",
        chandi:"",
        remark:""
    }
    //$('.unit-price').focus();
    /**
     * 表单提交验证
     */
    $scope.submit=function(){
        if($scope.product.pname==""||$scope.product.provider==""){
            alert("请输入商品名和采购商家");
        }else if(parseFloat($scope.product.unit_num_price)>0){
            if(parseFloat($scope.product.total_num)>0){
                $("#submit").click();
            }else{
                alert("请输入总件数");
            }
        }else if(parseFloat($scope.product.unit_weight_price)>0){
            if(parseFloat($scope.product.total_weight)>0){
                $("#submit").click();
            }else{
                alert("请输入总重");
            }
        }else{
            alert("请输入单价");
        }
    }
    /**
     * 表单提交
     */
    $scope.listsubmit=function(){
        if(confirm("确定提交列表？")){
            CaigouFactory.ajaxsubmitgoods().then(function(d){
                $scope.product=[];
            });
        }
    }
    $scope.detail_init=function(id){
            CaigouFactory.ajaxgetgoodinfo(id).then(function(d){
                $scope.product=d;
            })
    }
}]);
grErp.factory('CaigouFactory',['$http','$q',function($http,$q){
    var service={};
    service.ajaxgetgoods = function(batch){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Caigou/Index/ajaxgetgoods",
            params:{batch:batch}
        }).success(function(data,status,headers,config){
            console.log(data);
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.ajaxdelgoods = function(id){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Caigou/Index/ajaxdelgoods",
            params:{id:id}
        }).success(function(data,status,headers,config){
            console.log(data);
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.ajaxsubmitgoods = function(){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Caigou/Index/ajaxsubmitgoods"
            //params:{id:id}
        }).success(function(data,status,headers,config){
            console.log(data);
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.ajaxgetgoodinfo=function(id){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Caigou/Index/ajaxgetgood",
            params:{id:id}
        }).success(function(data,status,headers,config){
            console.log(data);
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    }
    return service;
}]);
